package cn.stronglink.assetmanage.business.check.model;

import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.dao.AssetDao;
import cn.stronglink.assetmanage.dao.CheckDao;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.Check;
import cn.stronglink.assetmanage.entity.Task;
import cn.stronglink.assetmanage.utils.Constants;

/**
 * 待入库数据操作
 * Created by chencongcong on 2018/6/14.
 */

public class WaitAssetModel {
    /**
     * 根据表单号获得该表单中的未入库数据  3为未入库
     *
     * @param orderNo
     * @return
     */
    public List<Asset> getDataByOrderNo(String orderNo) {
        List<Asset> assetList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getAssetDao().
                queryBuilder().where(AssetDao.Properties.OrderNo.eq(orderNo)).list();
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
        if (assetList != null) {
            return assetList;
        }
        return null;
    }

    /**
     * 根据表单号获得该表单号信息
     *
     * @param orderNo
     * @return
     */
    public Check getCheckByOrderNo(String orderNo) {
        List<Check> checkList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getCheckDao().queryBuilder().
                where(CheckDao.Properties.Id_.eq(orderNo)).
                where(CheckDao.Properties.Status.eq(Constants.STATUS_THREE)).list();
        if (checkList != null && checkList.size() > 0) {
            Check check = checkList.get(0);
            DaoManager.getDaoSession(MyApplication.APP_CONTEXT).clear();
            if (check != null) {
                return check;
            }
        }
        return null;
    }

    /**
     * 点击确定button将未入库的单子改为已经入库的状态，并给出dialog提示用户.3为未入库，4为已入库
     *
     * @param check
     */
    public void saveCheckByButton(Check check) {
        if (check != null) {
            //将单子列表改为已经入库状态
            List<Asset> assetList = getDataByOrderNo(check.getId_());
            check.setAssetList(assetList);
            check.setStatus(Constants.STATUS_FOUR);
            DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getCheckDao().insertOrReplace(check);
        }
    }
}
